package com.easytake.emotioncat.controller;

import com.easytake.emotioncat.dto.Response;
import com.easytake.emotioncat.entity.EmUserInfoEntity;
import com.easytake.emotioncat.fileio.FileUtil;
import com.easytake.emotioncat.fileio.minio.MinioConfig;
import com.easytake.emotioncat.param.UserParams;
import com.easytake.emotioncat.service.IEmUserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Optional;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2022-10-23
 */
@Tag(name = "用户接口")
@RestController
@RequestMapping("/user")
public class EmUserInfoController {
    @Autowired
    IEmUserInfoService userInfoService;

    @Autowired
    FileUtil fileUtil;

    @Autowired
    MinioConfig minioConfig;

    @Operation(summary = "更新用户")
    @PutMapping()
    public Response<EmUserInfoEntity> update(@RequestHeader String wx_uid, @RequestBody @Validated UserParams userInfo) {
        EmUserInfoEntity entity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(entity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        BeanUtils.copyProperties(userInfo, entity);
        userInfoService.updateById(entity);
        return Response.success(entity);
    }

    @Operation(summary = "添加用户")
    @PostMapping()
    //获取token时候自动创建，前端无需再创建
    public Response<EmUserInfoEntity> save(@RequestHeader String wx_uid, @RequestBody @Validated UserParams userInfo) {
        EmUserInfoEntity entity = userInfoService.queryByUserId(wx_uid);
        if (Objects.nonNull(entity)) {
            throw new IllegalArgumentException("用户已经存在");
        }
        entity = userInfoService.queryByPhone(userInfo.getPhone());
        if (Objects.nonNull(entity)) {
            throw new IllegalArgumentException("电话已经注册");
        }
        entity = new EmUserInfoEntity();
        BeanUtils.copyProperties(userInfo, entity);
        entity.setUserId(wx_uid);
        LocalDateTime now = LocalDateTime.now();
        entity.setCreateTime(now);
        entity.setModifyTime(now);
        userInfoService.save(entity);
        return Response.success(entity);
    }

    @Operation(summary = "查询用户")
    @GetMapping()
    public Response<EmUserInfoEntity> get(@RequestHeader String wx_uid) {
        EmUserInfoEntity userInfo = userInfoService.queryByUserId(wx_uid);
        if (Objects.nonNull(userInfo)) {
            if (StringUtils.isNoneBlank(userInfo.getImage())) {
                userInfo.setImage(fileUtil.generateFileUrl(userInfo.getImage()));
            }
        }
        return Response.success(userInfo);
    }

    @Operation(summary = "删除用户")
    @DeleteMapping
    public Response delete(@RequestHeader String wx_uid) {
        userInfoService.removeByUserId(wx_uid);
        return Response.success();
    }

    @Operation(summary = "设置用户头像")
    @PostMapping("/headImage")
    @Parameter(name = "image", description = "使用minio保存文件时候不传,前端上传文件服务器时候传递路径")
    @Parameter(name = "file", description = "使用minio保存文件时候传,前端上传文件服务器时候不用这个参数")
    public Response setHeadImage(@RequestHeader String wx_uid, String image, MultipartFile file) {
        EmUserInfoEntity entity = userInfoService.queryByUserId(wx_uid);
        if (Objects.isNull(entity)) {
            throw new IllegalArgumentException("用户不存在");
        }
        if (minioConfig.getEnable()) {
            String path = fileUtil.upload(file);
            String oldImage = entity.getImage();
            Optional.ofNullable(oldImage).ifPresent(i -> fileUtil.remove(i));
            entity.setImage(path);
        } else {
            entity.setImage(image);
        }
        userInfoService.updateById(entity);
        return Response.success();
    }

}
